home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C & C++ Multimedia Cyber Classroom
/
C and C++ Multimedia Cyber Classroom (Prentice Hall) (1998).iso
/
src
/
fig21_02.jar
/
Ch21
/
Fig21_02
/
fig21_02.cpp
Wrap
C/C++ Source or Header
|
1997-11-06
|
835b
|
39 lines
// Fig. 21.2: fig21_02.cpp
// Demonstrating the static_cast operator.
#include <iostream.h>
class BaseClass {
public:
void f( void ) const { cout << "BASE\n"; }
};
class DerivedClass: public BaseClass {
public:
void f( void ) const { cout << "DERIVED\n"; }
};
void test( BaseClass * );
int main()
{
// use static_cast for a conversion
double d = 8.22;
int x = static_cast< int >( d );
cout << "d is " << d << "\nx is " << x << endl;
BaseClass base; // instantiate base object
test( &base ); // call test
return 0;
}
void test( BaseClass *basePtr )
{
DerivedClass *derivedPtr;
// cast base class pointer into derived class pointer
derivedPtr = static_cast< DerivedClass * >( basePtr );
derivedPtr->f(); // invoke DerivedClass function f
}